package graduate.cslg.com.universityshopping;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FeedBackActivity extends AppCompatActivity {

    private EditText contentEdit;
    private EditText communicateEdit;
    private RadioGroup adviceType;
    private RadioButton contentAdvice;
    private RadioButton productAdvice;
    private RadioButton techAdvice;
    private RadioButton otherAdvice;
    private Button submitButton;
    private String getAdviceType;
    private LoginResponse mLoginResponse;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);

        initViews();

        adviceType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == contentAdvice.getId()) {
                    getAdviceType = "内容建议";
                } else if (checkedId == productAdvice.getId()) {
                    getAdviceType = "产品建议";
                } else if (checkedId == techAdvice.getId()) {
                    getAdviceType = "技术问题";
                } else if (checkedId == otherAdvice.getId()){
                    getAdviceType = "其他";
                }
            }
        });
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (contentEdit.getText().toString().length() != 0 && communicateEdit.getText().toString().length() != 0) {
                    showProgressDialog();
                    submitAdvice(getAdviceType, contentEdit.getText().toString(), communicateEdit.getText().toString());
                } else {
                    Toast.makeText(FeedBackActivity.this, "信息输入不完整！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initViews() {
        contentEdit = findViewById(R.id.advice_content);
        communicateEdit = findViewById(R.id.user_communicate);
        adviceType = findViewById(R.id.advice_type);
        contentAdvice = findViewById(R.id.content_advice);
        productAdvice = findViewById(R.id.product_advice);
        techAdvice = findViewById(R.id.tech_advice);
        otherAdvice = findViewById(R.id.other_advice);
        submitButton = findViewById(R.id.submit_button);
    }

    private void submitAdvice(String feedType, String feedContent, String feedCommunicate) {
        RetrofitDevelop.getTestService()
                .addFeedbackInfo(feedType, feedContent, feedCommunicate)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(FeedBackActivity.this, "反馈成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(FeedBackActivity.this, "反馈失败，请联系管理员", Toast.LENGTH_SHORT).show();
                        }
                        closeProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(FeedBackActivity.this, "网络错误，请稍后再试", Toast.LENGTH_SHORT).show();
                        closeProgressDialog();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }

    /**
     * 显示进度对话框
     */
    private void showProgressDialog(){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(this);
            progressDialog.setMessage("信息登记中，请稍等");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }
    /**
     * 关闭进度对话框
     */
    private void closeProgressDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }
}
